/*
 *  Medieval Melee
 *  Copyright (C) 2009 Ben Foote
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *  Uzebox is a reserved trade mark
*/

#ifndef UNIT_TEST
    #include <stdbool.h>
    #include <avr/io.h>
    #include <stdlib.h>
    #include <avr/pgmspace.h>
    #include <uzebox.h>
#else
    #include "unit_test.h"
#endif

#include "data/patches.inc"
#include "data/fliptrisTiles.inc"
#include "data/fonts_8x8.pic.inc"
#include "data/cursor.inc"
#include "data/maps.inc"

#include "globals.h"
#include "graphics.h"
#include "logic.h"
#include "AI.h"


/// These variables keep track of our gametime for scoring purposes.
unsigned char ucHzCounter = 0;
unsigned char ucSecondCounter = 0;
unsigned char ucMinuteCounter = 0;

/// If a match has been found or the screen needs to be updated, this
/// will indicate the change needs to be drawn.
bool bChanged = true;

/// Function prototypes.
void ManageTime(void);
unsigned int randomizer(void);
void processMouseMovement(PLAYFIELD_TYPE *tPlayfield, CURSOR_POS *tCurs);

////////////////////////////////////////////////////////////////////////////////////////////////////
/// Main function, responsible for initialization and running the games main loop.
////////////////////////////////////////////////////////////////////////////////////////////////////
int main(void)
{
    /// This is the 6x12 playfield of blocks made up of our PLAYFILED_TYPE struct.
    PLAYFIELD_TYPE tPlayfield;

    /// Holds player 1's cursor position on the screen.
    CURSOR_POS tCurs1;

	InitMusicPlayer(patches);
	
	SetTileTable(fliptrisTiles);
	SetFontTilesIndex(TILESET_SIZE);
	SetSpritesTileTable(cursor);
	ClearVram();
	
	Print(1,10, PSTR("PRESS START!"));
	WaitVsync(1);
    srand(randomizer());    
	LGC_InitializePlayfield(&tPlayfield, 6);
	LGC_GravityCheck(&tPlayfield);
	
	GRPH_DrawPlayfield(tPlayfield, 0);
	MapSprite(0,map_cursor);

    tCurs1.ucXpos = 0;
    tCurs1.ucYpos = 0;
    
    AI_Init();

	//while(1)
	{
		WaitVsync(1);

   	    PrintByte(4,0,ucSecondCounter, false);
   	    PrintByte(2,0,ucMinuteCounter, false);
   	    
   	    PrintByte(10,0,tCurs1.ucXpos, false);
  	    PrintByte(10,1,tCurs1.ucYpos, false);
        //processMouseMovement(&tPlayfield, &tCurs1);

        if(bChanged == true)
        {   
            LGC_GravityCheck(&tPlayfield);
            
            if(LGC_CheckForMatches(&tPlayfield))
                LGC_RemoveMatches(&tPlayfield);
                
            LGC_GravityCheck(&tPlayfield);
            GRPH_DrawPlayfield(tPlayfield,0);
            bChanged = false;
        }	
        
        ManageTime();           
	}
}




//Increment our time, must be called every Vsync to work
//properly.
void ManageTime(void)
{
    ucHzCounter++;
        
    if(ucHzCounter >= 30)
    {
        ucSecondCounter++;
        ucHzCounter = 0;
        
        if(ucSecondCounter >= 60)
        {                
            //if(AddRow() == false)
            //    SoftReset();
                
            ucSecondCounter = 0;
            ucMinuteCounter++;
            //bChanged = true;
        }
    }   
}

unsigned int randomizer()
{
    unsigned int uiRandom = 107;
    unsigned int uiJoy = 0;
    
    uiJoy = ReadJoypad(0);
    
    //while(!(uiJoy&BTN_START))
    {
        uiJoy = ReadJoypad(0);
        uiRandom++;
    }
    
    return uiRandom;
}


// Temporary function, FIXME.
void processMouseMovement(PLAYFIELD_TYPE *tPlayfield, CURSOR_POS *tCurs)
{
    //static unsigned char ucSoundCount = 6;
	unsigned int joy;

	//if(playDevice==0)
	//{
		//joy=ReadJoypad(playPort);
	joy=ReadJoypad(0);
    
	if(joy&BTN_LEFT)
	{
        if(tCurs->ucXpos > 0)
            tCurs->ucXpos-=1;

        while(ReadJoypad(0)!=0);
	}
	if(joy&BTN_RIGHT)
	{ 
        if(tCurs->ucXpos<4)
            tCurs->ucXpos+=1;

        while(ReadJoypad(0)!=0);
	}
	if(joy&BTN_UP)
	{
        if(tCurs->ucYpos > 0)
            tCurs->ucYpos-=1;

        while(ReadJoypad(0)!=0);           
	}
	if(joy&BTN_DOWN)
	{
		if(tCurs->ucYpos < 11)
            tCurs->ucYpos+=1;

        while(ReadJoypad(0)!=0);
	}
	
	if(joy&BTN_A)
	{
        while(ReadJoypad(0)!=0);
        
        LGC_SwapBlock(tPlayfield, tCurs->ucXpos, tCurs->ucYpos);
        bChanged = true;
	}

    if(joy&BTN_B)
    {
        while(ReadJoypad(0)!=0);
                        
        
        LGC_AddRow(tPlayfield);             
        bChanged = true;
    }
    
    if(joy&BTN_Y)
    {
        while(ReadJoypad(0)!=0);        
        PrintByte(2,1,AI_Think(tPlayfield, tCurs), false);
        bChanged = true;
                
        //TriggerFx(ucSoundCount, 0x88, false);
        
        //ucSoundCount++;
        
        //if(ucSoundCount > 9)
        //    ucSoundCount = 6;
    }

	MoveSprite(0, 8 + (tCurs->ucXpos*16), 32 + (tCurs->ucYpos*16), 4, 2);
}
